package jsyf.oa.web.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.DateUtils;
import jsyf.oa.facade.contract.ContractMainFacade;
import jsyf.oa.facade.contract.ReceivableFacade;
import jsyf.oa.facade.device.DeviceSampleFacade;
import jsyf.oa.facade.email.EmailFacade;
import jsyf.oa.facade.report.newAppReport.NewAppReportForBRMFacade;
import jsyf.oa.facade.report.wechatRelay.WechatShareFacade;
import jsyf.oa.facade.system.*;
import jsyf.oa.facade.task.BonusFacade;
import jsyf.oa.facade.task.SignRemindFacade;
import jsyf.oa.facade.thurTrack.ThurTrackFacade;
import jsyf.oa.facade.wechat.service_company.TokenFacade;
import jsyf.oa.facade.wreport.WReportFacade;
import jsyf.oa.web.sender.Sender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by y on 2018/4/30.
 */
@Component
public class Remind implements Serializable {
    public static final String ABSENCE_LIST = "您好!%1s缺勤名单如下：\n%2s\n %3s";
    public static final String WECHAT_REMIND = "晚上好!记得转发今天的易企秀哦！\n不要忘记上传好友截图哦！\n%1s";
    public static final String REPLAYMENT_REMIND = "您好!序列号为%1s的设备最近会有到期还款,请注意\n%2s";
    @Resource
    private TokenFacade tokenFacade;
    @Resource
    private SignRemindFacade signRemindFacade;
    @Resource
    private UserFacade userFacade;
    @Resource
    private BonusFacade bonusFacade;
    @Resource
    private DeviceSampleFacade deviceSampleFacade;
    @Resource
    private RegionAchivementsFacade regionAchivementsFacade;
    @Resource
    private ContractMainFacade contractMainFacade;
    @Resource
    private ReceivableFacade receivableFacade;
    @Resource
    private ThurTrackFacade thurTrackFacade;
    @Resource
    private MessageFacade messageFacade;
    @Resource
    private NewAppReportForBRMFacade newAppReportForBRMFacade;
    @Resource
    private EmailFacade emailFacade;
    @Resource
    private WechatShareFacade wechatShareRegionFacade;


    //@Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
    public void NormalRemindFront() {
        //System.out.println("所有人");
        //周一到周五一线发送签到提醒
        List<JSONObject> users = userFacade.getFrontRemindUser();
        StringBuilder sb = new StringBuilder();
        for (JSONObject user : users) {
            sb.append("|");
            sb.append(user.getString("id"));
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(0);
        }
        //System.out.println(sb);
        JSONObject object = signRemindFacade.getByRemark("定时签到", new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "1");
        if (object == null) {
            signRemindFacade.add("定时签到", "1");
            tokenFacade.SignRemind(sb);
        } else {
        }

    }

    public void NormalRemindBack() {
        //System.out.println("所有人");
        //周一到周五后台发送签到提醒
        List<JSONObject> users = userFacade.getBackRemindUser();
        StringBuilder sb = new StringBuilder();
        for (JSONObject user : users) {
            sb.append("|");
            sb.append(user.getString("id"));
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(0);
        }
        //System.out.println(sb);
        JSONObject object = signRemindFacade.getByRemark("定时签到", new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "0");
        if (object == null) {
            signRemindFacade.add("定时签到", "0");
            tokenFacade.SignRemind(sb);
        } else {
        }
    }

    public void SaturdayRemind() {
        //周六一线人员签到提醒
        //System.out.println("一线人员");
        List<JSONObject> users = userFacade.getFrontRemindUser();
        StringBuilder sb = new StringBuilder();
        for (JSONObject user : users) {
            sb.append("|");
            sb.append(user.getString("id"));
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(0);
        }
        //System.out.println(sb);
        JSONObject object = signRemindFacade.getByRemark("定时签到", new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "1");
        if (object == null) {
            signRemindFacade.add("定时签到", "1");
            tokenFacade.SignRemind(sb);
        } else {
        }
    }

    public void NormalAbsence() {
        //周一到周五缺勤提醒
        List<JSONObject> hrUsers = userFacade.getHRUser();
        //System.out.println("hr:"+hrUsers);
        JSONObject absence = signRemindFacade.getByRemark("缺勤提醒", new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "0");
        if (absence == null) {
            signRemindFacade.add("缺勤提醒", "0");
            for (JSONObject object : hrUsers) {
                List<JSONObject> users = userFacade.getAbsenceUser(object.getString("companyId"));
                StringBuilder sb = new StringBuilder();
                for (JSONObject user : users) {
                    sb.append(",");
                    sb.append(user.getString("name"));
                }
                if (sb.length() > 0) {
                    sb.deleteCharAt(0);
                }
                //System.out.println("ren:"+"hr"+object.getString("id")+sb);
                if (users != null && users.size() > 0) {
                    JSONObject msgObject = new JSONObject();
                    msgObject.put("userIds", object.getString("id"));//object.getString("id")
                    msgObject.put("content", String.format(ABSENCE_LIST, object.getString("companyName"), sb, DateUtils.parse(new Date(), "yyyy-MM-dd HH:mm")));
                    tokenFacade.sendTextMsg(JSON.toJSONString(msgObject));
                }
            }
        }
    }

    public void SaturdayAbsence() {
        //System.out.println("所有人");
        //周六缺勤提醒
        List<JSONObject> hrUsers = userFacade.getHRUser();
        //System.out.println("hr:"+hrUsers);
        JSONObject absence = signRemindFacade.getByRemark("缺勤提醒", new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "0");
        if (absence == null) {
            signRemindFacade.add("缺勤提醒", "0");
            for (JSONObject object : hrUsers) {
                List<JSONObject> users = userFacade.getAbsenceFrontUser(object.getString("companyId"));
                StringBuilder sb = new StringBuilder();
                for (JSONObject user : users) {
                    sb.append(",");
                    sb.append(user.getString("name"));
                }
                if (sb.length() > 0) {
                    sb.deleteCharAt(0);
                }
                //System.out.println("ren:"+"hr"+object.getString("id")+sb);
                if (users != null && users.size() > 0) {
                    JSONObject msgObject = new JSONObject();
                    msgObject.put("userIds", object.getString("id"));//object.getString("id")
                    msgObject.put("content", String.format(ABSENCE_LIST, object.getString("companyName"), sb, DateUtils.parse(new Date(), "yyyy-MM-dd HH:mm")));
                    tokenFacade.sendTextMsg(JSON.toJSONString(msgObject));
                }
            }
        }
    }

    public void WechatRemind() {
        //一线人员微信转发提醒
        //System.out.println("一线人员");
        List<JSONObject> users = userFacade.getALLFront();
        StringBuilder sb = new StringBuilder();
        for (JSONObject user : users) {
            sb.append("|");
            sb.append(user.getString("id"));
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(0);
        }
        //System.out.println(sb);
        JSONObject object = signRemindFacade.getByRemark("微信转发", new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "1");
        if (object == null) {
            signRemindFacade.add("微信转发", "1");
            JSONObject msgObject = new JSONObject();
            msgObject.put("userIds", sb);//object.getString("id")
            msgObject.put("content", String.format(WECHAT_REMIND, DateUtils.parse(new Date(), "yyyy-MM-dd HH:mm")));
            tokenFacade.sendTextMsg(JSON.toJSONString(msgObject));
        }
    }

    /**
     * 每周四商机跟踪提醒
     */
    public void WednesdayRemind() {
        JSONObject wednesdayRemind = signRemindFacade.getByRemark("每周三商机跟踪提醒",
                new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "1");
        if (wednesdayRemind == null) {
            signRemindFacade.add("每周三商机跟踪提醒", "1");
            thurTrackFacade.yuCeRemind();
        }
    }

    public void BirthdayRemind() {
        //员工生日
        //生日前一天提醒
        //List<JSONObject> users = userFacade.getBirthdayUsers(getdate(-1));//获取前一天提醒员工生日id
        //List<JSONObject> todayUsers = userFacade.getBirthdayUsers(getdate(0));
        JSONObject birthdayRemind = signRemindFacade.getByRemark("生日提醒", new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "0");
        if (birthdayRemind == null) {
            signRemindFacade.add("生日提醒", "0");
            signRemindFacade.birthdayRemind();
        }
    }

    //自动计算人员缺失惩罚
    public void BonusRemind() {
        System.out.println("++++++++++++++++++++++++++++开始+++++++++++++++++++++++++++++++");
        bonusFacade.updateBonusMoney("");

    }

    //到期还款提醒
    public void replaymentRemind() {
        JSONObject replayment = signRemindFacade.getByRemark("还款提醒", new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "0");
        if (replayment == null) {
            signRemindFacade.add("还款提醒", "0");
            List<JSONObject> users = userFacade.getUserBycondition(" AND roleName = '订货支持' AND jobState != 1");
            StringBuilder sb = new StringBuilder();
            for (JSONObject user : users) {
                sb.append("|");
                sb.append(user.getString("id"));
            }
            if (sb.length() > 0) {
                sb.deleteCharAt(0);
            }
            List<JSONObject> replayments = deviceSampleFacade.getRemindReplaymentList();
            StringBuilder sequences = new StringBuilder();
            for (JSONObject object : replayments) {
                sequences.append(",");
                sequences.append(object.getString("sequence"));
            }
            if (sequences.length() > 0) {
                sequences.deleteCharAt(0);
            }
            if (replayments != null && replayments.size() > 0) {
                JSONObject msgObject = new JSONObject();
                msgObject.put("userIds", sb);//object.getString("id")
                msgObject.put("content", String.format(REPLAYMENT_REMIND, sequences, DateUtils.parse(new Date(), "yyyy-MM-dd HH:mm")));
                tokenFacade.sendTextMsg(JSON.toJSONString(msgObject));
            }

        }
    }


    //区域经理季度配置
    public void regionMonthConfig() {
        JSONObject regionMonth = signRemindFacade.getByRemark("区域季度配置", new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "0");
        if (regionMonth == null) {
            signRemindFacade.add("区域季度配置", "0");
            regionAchivementsFacade.initDataByCurrentMonth();
        }
    }

    //更新每天区域经理人数
    public void regionList() {
        regionAchivementsFacade.initData();
    }

    public void TestCount() {
        regionAchivementsFacade.countCurrentMoney();
    }


    public String getdate(int i) // //获取前后日期 i为正数 向后推迟i天，负数时向前提前i天
    {
        Date dat = null;
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, -i);
        dat = cd.getTime();
        SimpleDateFormat dformat = new SimpleDateFormat("MM-dd");
        return dformat.format(dat);
    }
/*
    //给客户发送短信
    public void sender() {
        List<JSONObject> senderList = contractMainFacade.senderMessage();
        if (senderList != null && senderList.size() > 0) {
            for (int i = 0; i < senderList.size(); i++) {
                JSONObject sender = senderList.get(i);
                String customerName = sender.getString("customerName");
                String customerTel = sender.getString("customerTel");
                String brandName = sender.getString("brandName");
                String modelName = sender.getString("modelName");
                String deviceName = sender.getString("deviceName");
                String repaymentDate = sender.getString("repaymentDate");
                String serialNo = sender.getString("serialNo");
                String comName = "";
                switch (sender.getString("companyName")) {
                    case "黑龙江灜源":
                        comName = SendConstant.SENDER_HLJ;
                        break;
                    case "吉林清源":
                        comName = SendConstant.SENDER_JL;
                        break;
                    case "辽宁灜湖":
                        comName = SendConstant.SENDER_LN;
                        break;
                    case "江苏滚滚向前":
                        comName = SendConstant.SENDER_GGXQ;
                        break;
                    case "安徽灜湖":
                        comName = SendConstant.SENDER_AH;
                        break;
                    case "河北灜源":
                        comName = SendConstant.SENDER_HB;
                        break;
                    case "甘肃&宁夏":
                        comName = SendConstant.SENDER_GS;
                        break;
                    case "湖南清源":
                        comName = SendConstant.SENDER_HN;
                        break;
                    default:
                        break;
                }
                String content = String.format(SENDER_CUS_REMIND,
                        customerName, brandName,
                        deviceName, modelName,
                        serialNo, repaymentDate);
                send(comName, SendConstant.SENDER_PWD, customerTel, content);
            }
        }
    }*/


    public void send(String comName, String comPwd, String phone, String content) {
        // 存放调用返回的结果,判断成功与否应该分析这个字符串
        String returnCode;
        // 这里修改成你自己的用户名和密码
        Sender sms = new Sender(comName, comPwd);
        // 这里修改成要发送的手机号码和发送内容s
        returnCode = sms.massSend(phone, content, null, null);
        String encode = null;
        try {
            encode = new String(returnCode.getBytes("GBK"), "GB2312");
            System.out.println(encode);
        } catch (Exception e) {
            System.out.println("异常");
        }
    }

    public void senderExpire() {
        receivableFacade.senderExpire();
    }

    public void thurTackRemind() {
        thurTrackFacade.getAllThurTrackList();
    }

    public void todayPlanRemind() {
        newAppReportForBRMFacade.getTodayPlanRemind();
    }

    public void firstVisitRemind() {
        newAppReportForBRMFacade.firstVisitRemind();
    }

    public void amVisitRemind() {
        newAppReportForBRMFacade.amVisitRemind();
    }

    public void receivableRemind() {
        receivableFacade.receivableRemind();
    }

    public void receivableRemindYq() {
        receivableFacade.receivableRemindYq();
    }

    public void getNotReadReport() {
        emailFacade.getNotReadReport();
    }

    public void messageForSignIn() {
        messageFacade.getMessageForSignIn();
    }

    public void messageForDaQuSignIn() {
        messageFacade.getMessageForDaQuSignIn();
    }

    public void messageForSupportSignIn() {
        messageFacade.getMessageForSupportSignIn();
    }

    public void regionRemind() {
        wechatShareRegionFacade.regionRemind();
    }

    public void bigRegionRemind() {
        wechatShareRegionFacade.bigRegionRemind();
    }

    public void saveOverByRepayment() {
        receivableFacade.saveOverByRepayment();
    }




}
